define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'test/testitem/index/test_id/' + Config.test_id,
                    // add_url: 'test/testitem/add',
                    edit_url: 'test/testitem/edit/test_id/' + Config.test_id,
                    del_url: 'test/testitem/del/test_id/' + Config.test_id,
                    multi_url: '',
                    import_url: 'test/testitem/import/test_id/' + Config.test_id,
                    table: 'test_item',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'itembank.type', title:"题型", searchList: {"judge":"判断题","onechoice":"单选题","manychoice":"多选题"}, formatter: Table.api.formatter.normal},
                        {field: 'itembank.title', title:"题目"},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'score', title: "分数"},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $("#random").on("click",function(e){
				Layer.confirm('随机将清空原有试卷！确定要随机生成吗？', {
					btn: ['确认','取消'] //按钮
				}, function(index){
					Layer.close(index);
					var content = Template('random_param', {});
					layer.open({
						type: 1,
						area: ['500px', '700px'], 
						content: content,
						shadeClose:true
					});
					
					Form.api.bindevent($("#random_param-form"), function (data, ret) {	
						setTimeout(function () {
							location.href = ret.url ? ret.url : "/";
						}, 1000);
					});
				
				}, function(){
					console.log("取消随机试卷");
				});
				
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        chooseitem: function () {
            $(".collapse__hd").on("click",function(e){
				var content = $(this).next(".collapse__bd").toggle(500);
			});
			
			$(".btn-selected").on("click",function(){
				var param = $("#chooseitem-form").serializeArray();
				Fast.api.ajax({
					url:"test/testitem/postChooseItem/test_id/" + Config.test_id,
                    data:param,
                    success: function(data){
                        if (data.code == 1) {
                            Toastr.success(data.msg);
                            parent.Layer.closeAll();
                            parent.location.reload();
                            // parent.$("ul.nav-tabs li.active a").trigger("click");
                            /* setTimeout(function () {
                                 parent.location.reload();
                             }, 3000);*/
                        } else {
                            Toastr.error(data.msg);
                            Layer.closeAll();
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        Toastr.error("网络错误，请稍后重试！");
                        console.log('提交请求的错误信息：' + errorThrown + XMLHttpRequest);
                    }
				});
			});
        },
    };
    return Controller;
});